
// 显示内存图表
let PssMemory = echarts.init(document.getElementById('pss_memory'));
let processCpuInfo = echarts.init(document.getElementById('cpu'));
let Fps = echarts.init(document.getElementById('fps'));
// let dat;
fpsoption = {
    // color: '#ff197f',
    dataZoom: [
        {
            id: 'dataZoomX',
            type: 'slider',
            xAxisIndex: [0],
            filterMode: 'filter'
        }],
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'line'
        }
    },
    legend: {
        data: ['游戏帧率', '游戏延迟次数', '最长延迟时间']
    },
    xAxis: {
        // type: 'time',//由于定义的now时间格式不是完整的时间格式（我需要用的是时分秒），也就是说我只截取了一部分，
        // 所以不能使用time类型，需要使用默认的类型才能显示出来，否则显示不出来
        splitLine: {
            show: false
        },
        data: getData()[3]
    },
    yAxis: {
        name: "单位(帧)",
        type: 'value',
        boundaryGap: [0, '100%'],
        splitLine: {
            show: false
        },
    },
    series: [{
        name: '游戏帧率',
        type: 'line',
        showSymbol: false,
        hoverAnimation: true,
        data: getData()[4]
    },
        {
            name: '游戏延迟次数',
            type: 'line',
            showSymbol: false,
            hoverAnimation: true,
            data: getData()[5]
        },
        {
            name: '最长延迟时间',
            type: 'line',
            showSymbol: false,
            hoverAnimation: true,
            data: getData()[6]
        }
    ],
};
pssoption = {
    // color: '#ff197f',
    dataZoom: [
        {
            id: 'dataZoomX',
            type: 'slider',
            xAxisIndex: [0],
            filterMode: 'filter'
        }],
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'line'
        }
    },
    legend: {
        data: ['PSS内存', '系统内存']
    },
    xAxis: {
        // type: 'time',//由于定义的now时间格式不是完整的时间格式（我需要用的是时分秒），也就是说我只截取了一部分，
        // 所以不能使用time类型，需要使用默认的类型才能显示出来，否则显示不出来
        splitLine: {
            show: false
        },
        data: getData()[0]
    },
    yAxis: {
        name: "单位(Mb)",
        type: 'value',
        boundaryGap: [0, '100%'],
        splitLine: {
            show: false
        },
    },
    series: [{
        name: 'PSS内存',
        type: 'line',
        showSymbol: false,
        hoverAnimation: true,
        data: getData()[1]
    },
        {
            name: '系统内存',
            type: 'line',
            showSymbol: false,
            hoverAnimation: true,
            data: getData()[2]
        }
    ],
};
// let pssmemory;
cpuoption = {
    // color: '#ff197f',
    dataZoom: [
        {
            id: 'dataZoomX',
            type: 'slider',
            xAxisIndex: [0],
            filterMode: 'filter'
        }],
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'line'
        }
    },
    legend: {
        data: ['进程CPU', '系统CPU']
    },
    xAxis: {
        splitLine: {
            show: false
        },
        data: getData()[7],
    },
    yAxis: {
        name: "单位(%)",
        type: 'value',
        boundaryGap: [0, '100%'],
        splitLine: {
            show: false
        }
    },
    series: [{
        name: '进程CPU',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: getData()[8],
    },
        {
            name: '系统CPU',
            type: 'line',
            showSymbol: false,
            hoverAnimation: false,
            data: getData()[9]
        }
    ]
};

/*
获取服务端数据
*/
function getData() {
    let memory = perf_data.Memory;
    let cpu = perf_data.CPU;
    let fps = perf_data.FPS;
    let model = perf_model;
    let pssmemory;
    let x = [], y = [], z = [], xfps = [], yfps = [], zfps = [], tfps = [], xcpu = [], ycpu = [], syscpu = [];
    let str = "PSS内存";
    let str1 = "全局占用";
    let android_version = model["android_version"];
    let devicetype = model["manufacturer"];
    let devicename = model["name"];
    let sdk_version = model["sdk_version"];
    document.getElementById("deviceinfo").innerHTML = "<p class=\"devicetype\" id=\"devicetype\" style=\"color: #fae4f5\">" + devicetype + "</p>" +
        "<li class=\"item \" id=\"devicename\" style=\"color: #fae4f5;list-style-type:none;\">" + devicename + "</li>" +
        "<li class=\"item\" style=\"color: #fae4f5;list-style-type:none;\"> 安卓版本："+android_version +"</li>" +
        "<li class=\"item\" style=\"color: #fae4f5;list-style-type:none;\"> SDK版本："+sdk_version +"</li>";
    for (let key in memory) {
        if (key.search(str) !== -1) {
            pssmemory = memory[key];
            for (i = 0, len = pssmemory.length; i < len; i++) {
                a = formatDate(pssmemory[i]["a"]);
                x.push(a);
                y.push(Number(pssmemory[i]["b"].toFixed(2)));
            }
        }
        if (key.search(str1) !== -1) {
            pssmemory = memory[key];
            for (i = 0, len = pssmemory.length; i < len; i++) {
                z.push(Number(pssmemory[i]["b"].toFixed(2)));
            }
        }
    }
    for (let key in fps) {
        if (key.search("（游戏）帧率") !== -1) {
            _fps = fps[key];
            for (i = 0, len = _fps.length; i < len; i++) {
                a = formatDate(_fps[i]["a"]);
                xfps.push(a);
                yfps.push(Number(_fps[i]["b"].toFixed(2)));
            }
        }
        if (key.search("（游戏）延迟次数") !== -1) {
            _fps = fps[key];
            for (i = 0, len = _fps.length; i < len; i++) {
                zfps.push(Number(_fps[i]["b"].toFixed(2)));
            }
        }
        if (key.search("（游戏）最长延迟时间") !== -1) {
            _fps = fps[key];
            for (i = 0, len = _fps.length; i < len; i++) {
                tfps.push(Number(_fps[i]["b"].toFixed(2)));
            }
        }
    }
    for (let key in cpu) {
        if (key.search("-main-") !== -1) {
            _cpu = cpu[key];
            for (i = 0, len = _cpu.length; i < len; i++) {
                a = formatDate(_cpu[i]["a"]);
                xcpu.push(a);
                ycpu.push(Number(_cpu[i]["b"].toFixed(2)));
            }
        }
        if (key === "全局占用") {
            _cpu = cpu[key];
            for (i = 0, len = _cpu.length; i < len; i++) {
                syscpu.push(Number(_cpu[i]["b"].toFixed(2)));
            }
        }
    }

    return [x, y, z, xfps, yfps, zfps, tfps, xcpu, ycpu, syscpu]
}

//将时间戳转化成时间

function formatDate(timestamp) {
    let date = new Date(timestamp);
    let minutes = addZero(date.getMinutes());
    let seconds = addZero(date.getSeconds());
    return minutes + ':' + seconds;
}

//加0
function addZero(num) {
    return num < 10 ? '0' + num : num;
}

pssoption && PssMemory.setOption(pssoption);
cpuoption && processCpuInfo.setOption(cpuoption);
fpsoption && Fps.setOption(fpsoption);